package StudyCode.summary.zipfile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class IsZipFileByItsContent {
	public static void main(String[] argv) throws Exception {
		File f = new File("E:/Copy/zipdemo.zip");
		System.out.println("通过文件内容判断是不是 zip 文件 ? " + isZip(f));
	}

	public static boolean isZip(final File f) throws IOException {
		FileInputStream fis = null;
		boolean returnValue = false;
		try {
			fis = new FileInputStream(f);
			final CheckedInputStream cis = new CheckedInputStream(fis,
					new CRC32());
			final ZipInputStream zis = new ZipInputStream(
					new BufferedInputStream(cis));
			final ZipEntry ze = zis.getNextEntry();
			returnValue = false;

			if (ze != null) {
				returnValue = true;
			}
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
		return returnValue;
	}
}
